#!/usr/bin/python
# -*- coding: UTF-8 -*-
#查找文件并且拷贝文件到输入目录
#
# 文件名中包含字符的都会被拷贝到输出目录
# 参数：
# 1 查询目录 ：path="E:\\BaiduNetdiskDownload"
# 2 输出目录:  outpath="E:\\test"
# 3 查询名称：filename="测试文件名"
#Author  qq:563476701
#Last Modified: 2019-10-24 00:57
##
import os       
import sys      
import re
import shutil
    
def search(path,name):
    pathlist=[]
    for root, dirs, files in os.walk(path):  
        # path 为根目录
        for  filenames in files:
            if re.search(name, filenames, flags=re.I):                
                root = str(root)
                root=root.replace(path,"")
                pathlist.append(root+"\\"+filenames)      

    return pathlist
def copyfile(path,outpath):
    # 判断结果
    isExists=os.path.exists(outpath)         
    if not isExists:
        os.makedirs(outpath)
    f=open(outpath+"\\copyfile.txt","w")
    for pathfile in search(path,name):
        outpath+pathfile        
        tmpoutpath=os.path.dirname(outpath+"\\"+pathfile)
        isExists=os.path.exists(tmpoutpath) 
        # 判断结果
        if not isExists:
            # 如果不存在则创建目录
            # 创建目录操作函数
            os.makedirs(tmpoutpath)                    
        shutil.copyfile(path+"\\"+pathfile,outpath+"\\"+pathfile)
        f.write(outpath+"\\"+pathfile)
        f.write("\n")
    f.close()
# path = input('请输入您要查找哪个盘中的文件（如：D:\\\）')
# print('请输入您要查找的文件名：')
path="E:\\BaiduNetdiskDownload"
outpath="E:\\test"
filename="破解文件"
# name = sys.stdin.readline().rstrip()  #标准输入,其中rstrip()函数把字符串结尾的空白和回车删除
name=r'(.*)'+filename+'(.*)'
copyfile(path,outpath)
